home *** CD-ROM | disk | FTP | other *** search
- package horst;
-
- import java.awt.AWTEvent;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Point;
- import java.awt.Rectangle;
- import javax.swing.JComponent;
- import javax.swing.JScrollPane;
- import javax.swing.border.EmptyBorder;
-
- public class HTMLWindow extends JScrollPane implements HTMLPaneStatusListener {
- protected HTMLPane m_renderer;
- protected String m_findText;
- protected int m_lastFindPos;
-
- public HTMLWindow() {
- this.m_findText = "";
- this.m_renderer = this.createHTMLPane();
- ((JScrollPane)this).setViewportView(this.m_renderer);
- ((JComponent)this).setBackground(Color.white);
- ((JScrollPane)this).getViewport().setBackingStoreEnabled(true);
- }
-
- HTMLWindow(boolean bBorder) {
- this();
- if (!bBorder) {
- ((JComponent)this).setBorder(new EmptyBorder(0, 0, 0, 0));
- }
-
- }
-
- protected HTMLPane createHTMLPane() {
- return new HTMLPane();
- }
-
- public int find(String text) {
- int pos = this.m_renderer.findText(text, -1);
- if (pos > -1) {
- this.m_lastFindPos = pos;
- this.m_findText = text;
- this.highlightText(pos);
- }
-
- return pos;
- }
-
- public int findNext() {
- int pos = this.m_renderer.findText(this.m_findText, this.m_lastFindPos);
- if (pos > this.m_lastFindPos) {
- this.m_lastFindPos = pos;
- this.highlightText(pos);
- }
-
- return pos;
- }
-
- public HTMLPane getHTMLPane() {
- return this.m_renderer;
- }
-
- protected void highlightText(int pos) {
- View v = this.m_renderer.m_rootView.modelToView(pos);
- if (v != null) {
- this.m_renderer.m_document.m_selectedP0 = pos;
- this.m_renderer.m_document.m_selectedP1 = pos + this.m_findText.length() - 1;
- Rectangle vb = v.getBounds();
- Rectangle vr = ((JScrollPane)this).getViewport().getViewRect();
- if (vr.x <= vb.x && vr.y <= vb.y && vr.x + vr.width >= vb.x + vb.width && vr.y + vr.height >= vb.y + vb.height) {
- ((Component)this).repaint();
- } else {
- this.scrollToPosition(pos);
- }
- }
-
- }
-
- public void scrollToPosition(int pos) {
- View v = this.m_renderer.m_rootView.modelToView(pos);
- if (v != null) {
- Rectangle r = v.getBounds();
- if (r != null) {
- Rectangle docRect = this.m_renderer.m_rootView.getBounds();
- Dimension portDim = ((JScrollPane)this).getViewport().getExtentSize();
- int yOffset = 20;
- int xOffset = 20;
- int portY = Math.max(0, r.y - yOffset);
- int portX = Math.max(0, r.x - xOffset);
- if (portX + portDim.width > docRect.width) {
- int diff = portX + portDim.width - docRect.width;
- portX -= diff;
- if (portX < 0) {
- portX = 0;
- }
- }
-
- if (portY + portDim.height > docRect.height) {
- int diff = portY + portDim.height - docRect.height;
- portY -= diff;
- if (portY < 0) {
- portY = 0;
- }
- }
-
- ((JScrollPane)this).getViewport().setViewPosition(new Point(portX, portY));
- }
- }
-
- }
-
- public void scrollToVisible(int pos) {
- View v = this.m_renderer.m_rootView.modelToView(pos);
- if (v != null) {
- Rectangle vb = v.getBounds();
- Rectangle vr = ((JScrollPane)this).getViewport().getViewRect();
- if (vr.x <= vb.x && vr.y <= vb.y && vr.x + vr.width >= vb.x + vb.width && vr.y + vr.height >= vb.y + vb.height) {
- ((Component)this).repaint();
- } else {
- this.scrollToPosition(pos);
- }
- }
-
- }
-
- public void scrollToYPosition(int y) {
- Point pt = ((JScrollPane)this).getViewport().getViewPosition();
- ((JScrollPane)this).getViewport().setViewPosition(new Point(pt.x, y));
- ((JScrollPane)this).getViewport().repaint();
- }
-
- public void setHTMLPane(HTMLPane pane) {
- this.m_renderer = pane;
- ((JScrollPane)this).setViewportView(this.m_renderer);
- }
-
- public boolean statusChanged(HTMLPaneStatusEvent evt) {
- switch (((AWTEvent)evt).getID()) {
- case 5:
- this.m_lastFindPos = 0;
- default:
- return true;
- }
- }
- }
-